package rank

import (
	"context"
	"fmt"
	"strconv"

	"go-common/app/interface/main/app-show/model"
	"go-common/app/interface/main/app-show/model/region"
	"go-common/app/service/main/archive/api"
	"go-common/library/log"
)

func (s *Service) loadAuditCache() {
	as, err := s.adt.Audits(context.TODO())
	if err != nil {
		log.Error("s.adt.Audits error(%v)", err)
		return
	}
	s.auditCache = as
}

// Audit check audit plat and ip, then return audit data.
func (s *Service) Audit(c context.Context, mobiApp, order string, plat int8, build, rid int) (res []*region.ShowItem, ok bool) {
	if plats, ok := s.auditCache[mobiApp]; ok {
		if _, ok = plats[build]; ok {
			res = s.auditData(c, plat, rid, _auditRankAids, order)
			return res, true
		}
	}
	return nil, false
}

// auditData some data for audit.
func (s *Service) auditData(c context.Context, p int8, rid int, auditAids map[string][]int64, order string) (res []*region.ShowItem) {
	key := fmt.Sprintf(_initRank, order, rid)
	aids := auditAids[key]
	// archive
	as, err := s.arc.ArchivesPB(c, aids)
	if err != nil {
		log.Error("s.arc.ArchivesPB() error(%v)", err)
		as = map[int64]*api.Arc{}
	}
	for _, aid := range aids {
		if aid == 0 {
			continue
		}
		item := &region.ShowItem{}
		item.Goto = model.GotoAv
		item.Param = strconv.FormatInt(aid, 10)
		item.URI = model.FillURI(item.Goto, item.Param, nil)
		if a, ok := as[aid]; ok {
			item.Title = a.Title
			item.Cover = a.Pic
			item.Name = a.Author.Name
			item.Play = int(a.Stat.View)
			item.Danmaku = int(a.Stat.Danmaku)
			item.Reply = int(a.Stat.Reply)
			item.Fav = int(a.Stat.Fav)
		}
		res = append(res, item)
	}
	return
}

var (
	_auditRankAids = map[string][]int64{
		// rid
		"rank_key_all_13":   []int64{2434272, 7408756, 2426501, 2425990, 2387429, 2425770, 2419790, 2425440, 2414401, 2414476, 2402042, 7411278, 7418371},
		"rank_key_all_1":    []int64{104614, 190257, 199852, 233649, 258271, 275039, 274410, 286678, 291630, 292757, 299622, 306718, 430248, 432195, 462832, 476328, 508255, 524615, 539880, 559050, 560190, 572499, 576101, 575891, 582598, 588108, 591021, 602868, 620545, 627451, 627197, 621797, 630172, 663583, 666971, 747286, 744310, 789570, 824454, 832270, 851414, 853831, 874099, 884419, 880538, 2460323, 2459108, 2469560, 2471090},
		"rank_key_all_3":    []int64{8363, 209887, 221107, 322800, 333069, 360940, 361637, 400161, 402851, 465230, 454882, 462766, 482844, 539600, 595218, 644461, 644935, 756287, 785484, 801851, 850424, 853895, 875374, 881693, 1964781, 1966586, 1966643, 1969527, 1968681, 1986904, 1986802, 1984971, 1984330, 2294239, 2473751, 2473083},
		"rank_key_all_129":  []int64{886841, 936016, 966192, 1406019, 1773160, 1935680, 1958897, 1960912, 1964367, 1976153, 1985297, 1984555},
		"rank_key_all_4":    []int64{846437, 862006, 861854, 861796, 861711, 861409, 861209, 861124, 860863, 860273, 860187, 859930, 859374, 859264, 859132, 866115, 866091, 865870, 865843, 865610, 865511, 865442, 865388, 865318, 865259, 865076, 864958, 864845, 864737, 864732, 864700, 864528, 864390, 864360, 864344, 864264, 864121, 863929, 864969, 864762},
		"rank_key_all_36":   []int64{403120, 580862, 834839, 844027, 862316, 876708, 875624, 872364, 873174, 880857, 1971484, 1969254, 1968901, 1986932, 1985610, 2312249, 2310494, 2313588, 2316089, 2317272},
		"rank_key_all_5":    []int64{2318219},
		"rank_key_all_119":  []int64{78287, 638240, 913421, 1625784, 1636345, 1727650, 1743126, 1911041, 1959692, 1975358, 1976535, 1979757, 1982533, 1986533, 1985956},
		"rank_key_all_23":   []int64{1974120, 2038896, 2006455, 1957548, 377264, 1983046, 1955773, 1933830},
		"rank_key_all_155":  []int64{4991648},
		"rank_key_all_160":  []int64{8141, 7790, 7698, 6608, 6510, 6336, 6234, 5876, 5464, 5309, 5242, 5230, 5222, 4914, 10040, 9649, 8996, 8993, 11239, 11472, 11787, 12243, 12182, 12727, 15347, 16900, 16762, 17907, 18888, 200595, 288592, 286616, 339727, 393459, 517475, 530016, 551985, 555395, 581574, 588820, 592772, 592586, 633358, 648300, 659978, 666026, 658894, 658644, 658110, 667016, 668533, 668054, 673692, 673220, 672850, 670544, 677766, 680535, 680029, 670137, 679027, 695862, 686220, 685223, 689694, 690522, 702762, 698339, 696603, 701297, 707328, 700111, 708986, 708474, 699555, 699508},
		"rank_key_all_11":   []int64{1916941, 2031210, 2034983, 2015734, 2016150, 1964927, 1961205, 2039658},
		"rank_key_all_33":   []int64{2434272},
		"rank_key_all_0":    []int64{104614, 190257, 199852, 233649, 258271, 275039, 274410, 286678, 291630, 292757, 299622, 306718, 430248, 432195, 462832, 476328, 508255, 524615, 539880, 559050, 560190, 572499, 576101, 575891, 582598, 588108, 591021, 602868, 620545, 627451, 627197, 621797, 630172, 663583, 666971, 747286, 744310, 789570, 824454, 832270, 851414, 853831, 874099, 884419, 880538, 2460323, 2459108, 2469560, 2471090, 209887, 221107, 322800, 333069, 360940, 361637, 400161, 402851, 465230, 454882, 462766, 482844, 539600, 595218, 644461, 644935, 756287, 785484, 801851, 850424, 853895, 875374, 881693, 1964781, 1966586, 1966643, 1969527, 1968681},
		"rank_key_origin_0": []int64{291630, 299622, 364103, 430248, 432195, 462832, 508255, 539880, 559050, 560190, 576101, 582598, 588108, 620545, 627451, 627197, 630172, 824454, 832270, 851414, 874099, 884419, 880538, 2460323, 2459108, 2471090, 322800, 333069, 360940, 361637, 400161, 402851, 465230, 454882, 462766, 482844, 595218, 644461, 644935, 756287, 785484, 836018, 850424, 853895, 875374, 881693, 1966643, 1969527, 1968681, 1986904, 1986802, 1984330, 2473751, 2473083, 861854, 866115, 866091, 865843, 865610, 865511, 865442, 865388, 865318, 864958, 864845, 864737, 864700, 864528, 864390, 864360, 864344, 864264, 864121, 863929, 864705, 1977493, 1982920, 2312184, 2316655, 2314523},
	}
)
